#include "CDecartTree.h"
#include <iostream>
using namespace std;
#include <string>

int main()
{
	string s;
	int c;
	CDecartTreeArray<int> tree;
	int arg1, arg2;
	while( ( c = getchar() ) != EOF ) {
		while( c != ' ' ) {
			s.push_back( c );
			c = getchar();
		}
		if ( !s.compare( "insert" ) ) {
			cin>>arg1>>arg2;
			tree.Insert( arg1, arg2 );
		}
		else if( !s.compare( "sum" ) ) {
			cin>>arg1>>arg2;
			int result = tree.GetSum( arg1, arg2 );
			cout<<result<<'\n';
		}
		else if( !s.compare( "add" ) ) {
			cin>>arg1;
			tree.Append( arg1 );
		}
		else if( !s.compare( "remove" ) ) {
			cin>>arg1;
			tree.Delete( arg1 );
		}
		else {
			cout<<"ERROR: unknown command"<<'\n';
			break;
		}
		c = getchar(); //for \n
		s.clear();
	}
	return 0;
}